#include "EntityCamera.h"
#include "WorldModel.h"
#include "Visualisation.h"
#include "Camera.h"
#include "EntityPlayer.h"
#include "tools.h"

CEntityCamera::CEntityCamera(float distanceZ, float distanceY):CWorldEntity(), m_distanceZ(distanceZ), m_distanceY(distanceY), 
							m_offsetYModeFPS((float)0.6), m_tempDistanceY(m_distanceY), m_amountMovementUpDown((float)0), 
							m_sensitivityMouse((float)0.5)
{
	m_pitch=0;
	m_roll=0;
	m_detectCollision=false;
}

CEntityCamera::~CEntityCamera(void){}

void CEntityCamera::doUpdate(void)
{
	m_position=WORLDMODEL->getPlayerPosition();
	m_yaw=WORLDMODEL->getPlayerYaw();

	float turn=(float)0.02;

	if(WORLDMODEL->getCameraMode()==CWorldModel::eModeTPS)
	{
		//////////calcul the offset of the camera on the Z axis
		D3DXVECTOR3 look(0, 0, 1);
		D3DXVECTOR3 up(0, 1, 0);
		D3DXMATRIX yawMatrix;

		//calcul a rotation matrix for the yaw
		D3DXMatrixRotationAxis(&yawMatrix, &up, m_yaw);

		//transform the matrix rotation into a vector
		D3DXVec3TransformCoord(&look, &look, &yawMatrix);
		D3DXVec3Normalize(&look, &look);

		m_position+=-look*m_distanceZ;

		/////////// calcul the Y offset
		if(m_amountMovementUpDown!=0)
		{
			//get the new offset
			m_tempDistanceY=m_distanceY;
			m_tempDistanceY+=turn*m_amountMovementUpDown*10;

			//check if it doesn't put the camera in a wrong place: too high or too low
			if(m_position.y+m_tempDistanceY>m_position.y && m_position.y+m_tempDistanceY<10)
			{
				m_distanceY=m_tempDistanceY;
			}
			m_amountMovementUpDown=0;
			//OutputDebugString("movementUpAndDown!=0\n");
		}

		//add the distance
		m_position.y+=m_distanceY;
	
		////////make the camera point to the player
		//m_pitch=0;
		m_pitch=(float)((3.14/2)-atan(m_distanceZ/m_distanceY));
		m_roll=0;
	}

	
	if(WORLDMODEL->getCameraMode()==CWorldModel::eModeFPS)
	{
		if(m_amountMovementUpDown!=0)
		{
			m_pitch+=turn*m_amountMovementUpDown;
			m_amountMovementUpDown=0;
		}
		m_position.y+=m_offsetYModeFPS;
	}

	//OutputDebugString("Camera: ");
	//displayPosition(m_position);
}
void CEntityCamera::doRender(void)
{
		VIS->calculCamera(m_position, m_pitch, m_yaw, m_roll);
}

void CEntityCamera::lookUpDown(float amount)
{
	m_amountMovementUpDown=amount*m_sensitivityMouse;
}